package com.luck.easyfoodbatch.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.luck.easyfoodbatch.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HttpActivity extends BaseActivity {
    @BindView(R.id.wb)
    WebView wb;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");
        setContentView(R.layout.activity_http, title, MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        wb.getSettings().setDefaultTextEncodingName("utf-8");
        wb.getSettings().setJavaScriptEnabled(true);
        wb.setWebChromeClient(new WebChromeClient());
        wb.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }
        });
        wb.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (wb != null) {
            wb.destroy();
        }
    }
}
